#downloader
require 'rubygems'
require 'hpricot'
require 'net/http'

#notifier
require 'win32/sound'
include Win32

require 'time'

module Mkuz

	class Notifier
		def run 
			Sound.play('tada.wav')
		end
	end
	
	class OnlinerByChecker
		def run
		
			data_link = 'http://baraholka.onliner.by/viewforum.php?f=62&cat=5&sk=created'
			data_uri = URI.parse(data_link)
		
			#puts "checking #{data_link}"
		
			Net::HTTP.start( data_uri.host, data_uri.port ) do |http|
                doc = Hpricot(http.get("#{data_uri.path}?#{data_uri.query}").body)
                posts = doc.search("//td.frst div.ph-i span.img-va a")
            
				#puts "posts=#{posts}"
				
				post = nil
				posts.each do |p| 
					post = p
					cls = p.parent.parent.parent.parent['class']
					break if cls.nil?
				end
				
				data = post['href']
				#puts "data=#{data}"
				
				if data != @last_data then 
					puts "NEW POST FOUND AT #{data_link}"
					Notifier.new.run
				end
				@last_data = data
            end
			
			#puts 'checking finished'
		end
	end
	
	class NeagentByChecker
		def run
		
			data_link = 'http://neagent.by/board/?formtype=kv&mode=search&sity=1&prtype=arenda&k1=on&k2=on&priceMin=150&priceMax=350&withcontent=&postdate=1&ar[]=&ar[]=&ar[]=&ar[]=&ar[]=&ar[]=&ar[]=&ar[]=&ar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]=&subar[]='
			data_uri = URI.parse(data_link)
		
			#puts "checking #{data_link}"
		
			Net::HTTP.start( data_uri.host, data_uri.port ) do |http|
                doc = Hpricot(http.get("#{data_uri.path}?#{data_uri.query}").body)
                post = doc.at("html body div#wrapper div#middle div#container div#content div.item")
                data = post.at("div.bord1 div.bord2 div.it_header div.it_3 div.it_date").inner_text
				
				if data != @last_data then 
					puts "NEW POST FOUND AT #{data_link}"
					Notifier.new.run
				end
				@last_data = data
            end
			
			#puts 'checking finished'
		end
	end


	class KvartiraMinskInNetChecker
	
		def run
			data_link = 'http://www.kvartira.minsk-in.net/showit.php?filter_go=1&money_from=150&money_to=350&filter_agents=3&new_filtr_kom[]=0&new_filtr_kom[]=1&new_filtr_raions[]=25&new_filtr_raions[]=31&new_filtr_raions[]=39&new_filtr_raions[]=45&new_filtr_raions[]=53&showlist=1&groupneed=1&charset=&idea='
			
			data_uri = URI.parse(data_link)
		
			#puts "checking #{data_link}"
		
			Net::HTTP.start( data_uri.host, data_uri.port ) do |http|
                doc = Hpricot(http.get("#{data_uri.path}?#{data_uri.query}").body)
				
				#"html body table#Table_01 tbody tr td div table tbody tr td table"
				#" br br table"
				
				data = nil
				tables = doc.search("//table");
				tables.each do |table| 
					id = table["id"]
					if not id.nil? and id.match(/^kv_mess_\d+/) then
						data = id
						break
					end
				end
				
				if data != @last_data then	
					puts "NEW POST FOUND AT #{data_link}"
				 	Notifier.new.run 
				end
				@last_data = data
            end
			
			#puts 'checking finished'
		end
	end


		class IrrByChecker
			def run
				run1
				run2
			end
			
			def parse(data_link)
				data_uri = URI.parse(data_link)
		
				#puts "checking #{data_link}"
			
				Net::HTTP.start( data_uri.host, data_uri.port ) do |http|
					doc = Hpricot(http.get("#{data_uri.path}?#{data_uri.query}").body)
					post = doc.at("html body div#minWidth.minWidth div.middle div.cont-col div.contcol div.b-content div.colMiddle div.b-adList table.adListTable tbody tr.advertRow")
					
					text = post.at("td.tdTxt p").inner_text
					data = text
					
					#advert_link = post.at("td.tdTxt div.h3 a")['href']
					
					#advert_uri = URI.parse(advert_link)
					#advert_doc = Hpricot(http.get("#{advert_uri.path}?#{advert_uri.query}").body)
					#data = advert_doc.at('html body div#minWidth.minWidth div.middle div.cont-col div.contcol div.b-content div.b-advert div.b-middle div.b-infAdvert div.wrap div.floatRight p').inner_text
					
					data
				end
				#puts 'checking finished'
			end
			
			def run1
				data_link = 'http://irr.by/realestate/longtime/search/offertype=%D0%BF%D1%80%D0%B5%D0%B4%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5/price=%D0%BE%D1%82%20150%20%D0%B4%D0%BE%20350/currency=USD/rooms=1/date_create=today/tab=users/'
				data = parse data_link
				if data != @last_data1 then
					puts "NEW POST FOUND AT #{data_link}"
					Notifier.new.run 
				end
				@last_data1 = data
			end
			def run2
				data_link = 'http://irr.by/realestate/longtime/search/offertype=%D0%BF%D1%80%D0%B5%D0%B4%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5/price=%D0%BE%D1%82%20150%20%D0%B4%D0%BE%20350/currency=USD/rooms=2/date_create=today/tab=users/'
				data = parse data_link
				if data != @last_data2 then
						puts "NEW POST FOUND AT #{data_link}"
						Notifier.new.run 
					end
					@last_data2 = data
			end
			
		end
		
		
	
	class HataByChecker
	
		def run
		
			data_link = 'http://www.hata.by/search/showcase/~cname=%D0%9C%D0%B8%D0%BD%D1%81%D0%BA~ckod=5000000000~ctype=ckod~s_what=flat~s_do=rent~gtype=belarus~rooms_from=1~rooms_to=2~cena_from=150~cena_to=340~currency=840~owner_only=on~added_days=12~search_type=small/page/1/'
			
			data_uri = URI.parse(data_link)
			
			#puts "checking #{data_link}"
			
				Net::HTTP.start( data_uri.host, data_uri.port ) do |http|
					doc = Hpricot(http.get("#{data_uri.path}?#{data_uri.query}").body)
					post = doc.at("html body div#wrapperOut div#wrapper div#middle div.rightContent div div.flatListItem")
					return if post.nil?
					id = post['id']
					
					data = id
					if data != @last_data then
						puts "NEW POST FOUND AT #{data_link}"
						Notifier.new.run 
					end
					@last_data = data
				end
				
			#puts 'checking finished'
		end
	end
	
	class FlatFinder 
		
		def run
		
			checkers = [ OnlinerByChecker.new, KvartiraMinskInNetChecker.new, HataByChecker.new, IrrByChecker.new, NeagentByChecker.new ]
		
			while true do
				time = DateTime.now.strftime('%d %b %H:%M')
				puts "checking at #{time}..."
			
				checkers.each do |checker| 
					begin
						checker.run 
					rescue
						puts "error in #{checker.class.name}: #{$!}"
					end
				end
				
				wait_for_secs = 60
				sleep(wait_for_secs)
			end
		end

	end

end #module


begin

finder = Mkuz::FlatFinder.new
finder.run

end